<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kat extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->library('form_validation');
	}

	public function index(){
		if ($this->session->userdata('isLoggedIn')){
            $menu['isLoggedIn'] = true;
        }else {
        	redirect('site');
        }
		$this->load->model('kategori','',true);
		$menu['kategori'] = $this->kategori->get_all();
		$this->load->view('header');
		$this->load->view('menu',$menu);
		$this->load->view('kategori',$menu);
		$this->load->view('footer');
	}

	public function insert(){
		if ($this->session->userdata('isLoggedIn')){
            $menu['isLoggedIn'] = true;
        }else {
        	redirect('site');
        }
		$namakat = $this->input->post('namakat');
		$this->load->model('kategori','',true);
		$this->form_validation->set_rules('namakat', 'namakat', 'trim|required|is_unique[kategori.namakat]');
		if ($this->form_validation->run() == FALSE) {
			$this->load->model('kategori','',true);
			$menu['kategori'] = $this->kategori->get_all();
			$menu['error'] = true;
			$this->load->view('header');
			$this->load->view('menu',$menu);
			$this->load->view('kategori',$menu);
			$this->load->view('footer');
		} else {
			$this->kategori->insert($namakat);
			redirect('kat');
		}
		
	}

	public function delete($data){
		if ($this->session->userdata('isLoggedIn')){
            $menu['isLoggedIn'] = true;
        }else {
        	redirect('site');
        }
		$this->load->model('kategori','',true);
		$this->kategori->delete($data);
		redirect('kat');
	}

	public function edit($kategori){
		if ($this->session->userdata('isLoggedIn')){
            $menu['isLoggedIn'] = true;
        }else {
        	redirect('site');
        }
		$this->load->model('kategori','',true);
		$menu['kategori'] = $this->kategori->get_all();
		$isi['detail'] = $this->kategori->get_by_id($kategori);
		$this->load->view('header');
		$this->load->view('menu',$menu);
		$this->load->view('kategori',$isi);
		$this->load->view('footer');
	}

	public function update(){
		if ($this->session->userdata('isLoggedIn')){
            $menu['isLoggedIn'] = true;
        }else {
        	redirect('site');
        }
		$data = array(
			'namakat' => $this->input->post('namakat')
			);
		$id = $this->input->post('idkategori');
		$this->load->model('kategori','',true);
		$this->form_validation->set_rules('namakat', 'namakat', 'trim|required|is_unique[kategori.namakat]');
		if ($this->form_validation->run() == FALSE) {
			$this->load->model('kategori','',true);
			$menu['kategori'] = $this->kategori->get_all();
			$menu['error'] = true;
			$this->load->view('header');
			$this->load->view('menu',$menu);
			$this->load->view('kategori',$menu);
			$this->load->view('footer');
		} else {
			$this->kategori->update($id,$data);
			redirect('kat');
		}
		
	}
}